package com.boardour.wifip2p.popup;

import android.app.Activity;
import android.content.DialogInterface;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.boardour.wifip2p.R;
import com.boardour.wifip2p.WifiP2pCommon;
import com.boardour.wifip2p.adapter.ActionListener;
import com.boardour.wifip2p.receiver.ConnectManager;
import com.boardour.wifip2p.receiver.IConnectListener;
import com.boardour.wifip2p.utils.LG;

import java.util.ArrayList;

import cn.carhouse.alert.QuickBuilder;
import cn.carhouse.alert.QuickDialog;

/**
 * Wifi列表弹窗
 */
public class WifiP2pListPopup implements IConnectListener, DialogInterface.OnDismissListener {
    private Activity activity;
    private QuickDialog dialog;
    private RecyclerView recyclerView;
    private WifiP2pListAdapter adapter;
    private ConnectManager connectManager;
    private boolean startWifiSetting;
    private ProgressBar progressBar;
    private DialogInterface.OnDismissListener onDismissListener;
    private boolean isRegister = false;

    public WifiP2pListPopup(Activity activity) {
        this(activity, false);
    }

    public WifiP2pListPopup(Activity activity, boolean startWifiSetting) {
        LG.isDebug = true;
        this.activity = activity;
        this.startWifiSetting = startWifiSetting;
        int height = activity.getResources().getDisplayMetrics().heightPixels;
        dialog = new QuickBuilder(activity)
                .fromBottom(true)
                .setFullWidth()
                .setHeight((height * 3) / 4)
                .setContentView(R.layout.wifip2p_list_dialog)
                .build();
        progressBar = dialog.findViewById(R.id.pb);
        recyclerView = dialog.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        adapter = new WifiP2pListAdapter(activity);
        recyclerView.setAdapter(adapter);

        dialog.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        connectManager = new ConnectManager();
        connectManager.setConnectListener(this);
        register();

        dialog.setOnDismissListener(this);
    }

    private void register() {
        if (!isRegister) {
            connectManager.registerBroadcastReceiver(activity);
            isRegister = true;
        }
    }

    private void unregister() {
        if (isRegister) {
            connectManager.unregisterBroadcastReceiver(activity);
            isRegister = false;
        }
    }

    public void show() {
        LG.e("show");

        if (dialog != null && !dialog.isShowing()) {
            register();
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onWifiP2pStateChanged(boolean wifiP2pEnable) {
        LG.e("onWifiP2pStateChanged");
        if (!wifiP2pEnable) {
            Toast.makeText(activity, "Wifi 未打开", Toast.LENGTH_SHORT).show();
            if (startWifiSetting) {
                WifiP2pCommon.getInstance().startWifiSetting();
            }
        }
        discoverPeers();

    }

    private void discoverPeers() {
        WifiP2pCommon.getInstance().discoverPeers(new ActionListener() {
            @Override
            public void onSuccess() {
                onPeersChanged();
            }

            @Override
            public void onFailure(int reason) {
                LG.e("discoverPeers:" + reason);
            }
        });
    }

    @Override
    public void onPeersChanged() {
        LG.e("onPeersChanged");
        WifiP2pCommon.getInstance().requestPeers(new WifiP2pManager.PeerListListener() {
            @Override
            public void onPeersAvailable(WifiP2pDeviceList peers) {
                if (peers == null) {
                    return;
                }
                if (peers.getDeviceList().size() > 0) {
                    progressBar.setVisibility(View.GONE);
                }
                adapter.replaceAll(new ArrayList<>(peers.getDeviceList()));
            }
        });

    }

    @Override
    public void onWifiP2pConnectionChanged(boolean wifiP2pConnect) {
        LG.e("onWifiP2pConnectionChanged");
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onWifiP2pDeviceChanged(WifiP2pDevice wifiP2pDevice) {
        LG.e("onWifiP2pDeviceChanged");
        if (wifiP2pDevice != null) {
            if (wifiP2pDevice.status == WifiP2pDevice.AVAILABLE) {
                discoverPeers();
            }
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        LG.e("onDismiss");
        WifiP2pCommon.getInstance().stopPeerDiscovery();
        unregister();
        if (this.onDismissListener != null) {
            this.onDismissListener.onDismiss(dialog);
        }
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        this.onDismissListener = listener;
    }
}
